﻿using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Collections.Generic;

namespace Perm.Web
{
    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class Comments
    {
        string select;
        SqlConnection connect;
        SqlDataReader reader;
        SqlCommand command;
        [OperationContract]
        public List<Comment> DoWork(string city, int id,string name,string comment, string type)
        {
            string data = DateTime.Today.ToLongDateString() + " " + DateTime.UtcNow.AddHours(6).ToShortTimeString();

            List<Comment> CommentList = new List<Comment>(); 
            string connectionString = WebConfigurationManager.ConnectionStrings["MSSQL"].ConnectionString;
            connect = new SqlConnection(connectionString);
            connect.Open();
            switch (type)
            {
                case "load":
                    switch (city)
                    {
                        case "perm":
                            {
                                select = "SELECT [NAME], [COMMENT], [DATA]  FROM [COMMENTS] WHERE [NO]=" + id + " ORDER BY [ID]";                     
                                break;
                            }
                        case "ekb":
                            {
                                select = "SELECT [NAME], [COMMENT], [DATA]  FROM [COMMENTS_EKB] WHERE [NO]=" + id + " ORDER BY [ID]";                     
                                break;
                            }
                        case "permkray":
                            {
                                select = "SELECT [NAME], [COMMENT], [DATA]  FROM [COMMENTS_PERMKRAY] WHERE [NO]=" + id + " ORDER BY [ID]";                     
                                break;
                            }
                    } 
                    
                    command = new SqlCommand(select, connect);
                    reader = command.ExecuteReader();
                    
                    if (reader.Read())
                    {
                        Comment commentLoad1 = new Comment();
                        commentLoad1.name = reader[0].ToString();
                        commentLoad1.comment = reader[1].ToString();
                        commentLoad1.data = reader[2].ToString();
                        CommentList.Add(commentLoad1);
                        while (reader.Read())
                            {
                                Comment commentLoad = new Comment();
                                commentLoad.name = reader[0].ToString();
                                commentLoad.comment = reader[1].ToString();
                                commentLoad.data = reader[2].ToString();
                                CommentList.Add(commentLoad);
                            }
                    }
                    else
                    {
                        Comment commentLoad = new Comment();
                        commentLoad.comment = "Коментариев пока нет";
                        CommentList.Add(commentLoad);    
                    }                    
                    connect.Close();
                    break;
                case "save":
                    switch (city)
                    {
                        case "perm":
                            {
                                select = "INSERT [COMMENTS] ([NO], [NAME], [COMMENT], [DATA]) VALUES (" + id + ", '" + name + "', '" + comment + "', '" + data + "')";                    
                                break;
                            }
                        case "ekb":
                            {
                                select = "INSERT [COMMENTS_EKB] ([NO], [NAME], [COMMENT], [DATA]) VALUES (" + id + ", '" + name + "', '" + comment + "', '" + data + "')";                    
                                break;
                            }
                        case "permkray":
                            {
                                select = "INSERT [COMMENTS_PERMKRAY] ([NO], [NAME], [COMMENT], [DATA]) VALUES (" + id + ", '" + name + "', '" + comment + "', '" + data + "')";                    
                                break;
                            }
                    }                     
                    command = new SqlCommand(select, connect);
                    command.ExecuteNonQuery();
                    connect.Close();
                    break;
            }            
            return CommentList;
        }
        [DataContract]
        public class Comment
        {
            [DataMember]
            public string name = "";
            [DataMember]
            public string comment = "";            
            [DataMember]
            public string data = "";
        }


        // Add more operations here and mark them with [OperationContract]
    }
}
